package com.example.springcloudexamineserver.config;

import com.example.springcloudexamineserver.service.ExecutePython;
import lombok.SneakyThrows;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import com.example.springcloudexamineserver.config.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;


import java.io.File;

public class FileListen extends FileAlterationListenerAdaptor {
//    WebSocketServer webSocketServer = new WebSocketServer();
    public ExecutePython executepython = new ExecutePython();

    public WebSocketServer webSocketServer = new WebSocketServer();

    private final RestTemplate restTemplate = new RestTemplate();
    private final String modelId = restTemplate.getForObject("http://localhost:7000/model/getModelId",String.class);
    private final String simility = ExecutePython.result;

    String savepath = "D:\\upload\\savepath\\";
    String savemodel = "D:\\upload\\model_gpi\\";

    @SneakyThrows
    @Override
    public void onFileCreate(File file) {
        String filename = file.getName().substring(0,file.getName().lastIndexOf('.'));
        //新建文件夹
        File dir = new File(savepath+filename);
        if(!dir.exists()){
            dir.mkdir();
        }
        //移动gpi文件
        File gpifile = new File(savepath+filename+"\\"+filename+"_Gpi");
        System.out.println(gpifile.getName());
        if (file.renameTo(new File (dir + "\\" + file.getName()))){
            System.out.println("");
        }
        String filepath;
        filepath = (dir + "\\" + file.getName());
        System.out.println(filepath);
        //清洗数据
        executepython.data_cleaning(filepath);
        if(gpifile.renameTo(new File (savemodel + gpifile.getName()))){
            System.out.println("");
        }
        System.out.println(gpifile);
        //执行kerel脚本
        System.out.println("开始执行Kerel算法计算相似度");
        String gpiDir = "D:\\upload\\model_gpi";
        executepython.kerel(gpiDir);
        System.out.println("计算结束!");
//        WebSocketServer.sendInfo(simility, WebSocketServer.getUserId);
//        WebSocketServer.sendInfo(modelId, WebSocketServer.getUserId);
        WebSocketServer.sendInfo("发送kerel消息",WebSocketServer.getUserId);
        WebSocketServer.sendInfo("id:" + modelId,WebSocketServer.getUserId);
//        WebSocketServer.sendInfo("id:9",WebSocketServer.getUserId);

    }

}
